#include "SimpleDockApplication.h"

//==============================================================================
SimpleDockApplication::SimpleDockApplication()
		: mainComponent (0),
		  guiActionsManager (0),
		  compositionManager (0),
		  configs (0)
    {
        // NEVER do anything in here that could involve any Juce function being called
        // - leave all your startup tasks until the initialise() method.
    }

SimpleDockApplication::~SimpleDockApplication()
{
        // Your shutdown() method should already have done all the things necessary to
        // clean up this app object, so you should never need to put anything in
        // the destructor.

        // Making any Juce calls in here could be very dangerous...
}

    //==============================================================================
void SimpleDockApplication::initialise (const String& commandLine)
{
	File configFile("res/config.cfg");
	if(configFile.exists())
	{
		configs = new PropertiesFile(File(configFile), 1000, PropertiesFile::storeAsBinary);
	}

	compositionManager = new CompositionManager();
	if(compositionManager->loadComposition("composition.cfg"))
	{
		// just create the main component...
		mainComponent = new MainComponent(compositionManager->getRoot());
		mainComponent->setTopLeftPosition(Desktop::getInstance().getMainMonitorArea().getWidth()/2 - mainComponent->getWidth()/2, 1 - mainComponent->getHeight());

		//adding to desktop...
		int flags = ComponentPeer::windowIsSemiTransparent;
		mainComponent->addToDesktop(flags,0);
		mainComponent->setVisible(true);
		mainComponent->setAlwaysOnTop(true);
		
//		mainComponent->setTopLeftPosition(100,100);

		guiActionsManager = new DefaultGUIActionsManager(mainComponent);			

		// Desktop::getInstance().addGlobalMouseListener(globalListener = new GlobalMouseListener(mainComponent));
	}
	else
	{
		//load default composition (BAD FILE)
		jassertfalse
	}

}

void SimpleDockApplication::shutdown()
{
    // clear up..
	if(guiActionsManager)
		delete guiActionsManager;

	if(compositionManager)
	{
		compositionManager->saveComposition(mainComponent->getRootContainer());
		delete compositionManager;
	}
	if (mainComponent != 0)
	{
        delete mainComponent;
	}

	if(configs)
	{
		configs->saveIfNeeded();
		delete configs;
	}
}

PropertySet * SimpleDockApplication::getApplicationProperties()
{
	return configs;
}

GUIActionsManager * SimpleDockApplication::getGUIActionsManager()
{
	return guiActionsManager;
}

MainComponent * SimpleDockApplication::getMainComponent()
{
	return mainComponent;
}

    //==============================================================================
const String SimpleDockApplication::getApplicationName()
{
    return T("Simple Dock");
}

const String SimpleDockApplication::getApplicationVersion()
{
    return T("1.0");
}

bool SimpleDockApplication::moreThanOneInstanceAllowed()
{
    return true;
}

void SimpleDockApplication::anotherInstanceStarted (const String& commandLine)
{
}
